package com.study.manage.mvc.v2;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.web.PageableDefault;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.study.manage.dal.BlogCommentMediaRepository;
import com.study.manage.dal.BlogCommentRepository;
import com.study.manage.dal.BlogGradeRepository;
import com.study.manage.dal.BlogRepository;
import com.study.manage.dal.EvaluateApplyRepository;
import com.study.manage.dal.EvaluateApplyWorkRepository;
import com.study.manage.dal.EvaluateJuryRepository;
import com.study.manage.dal.EvaluateRepository;
import com.study.manage.dal.UserDetailRepository;
import com.study.manage.dal.UserRepository;
import com.study.manage.domain.Blog;
import com.study.manage.domain.BlogComment;
import com.study.manage.domain.BlogGrade;
import com.study.manage.domain.Evaluate;
import com.study.manage.domain.EvaluateApply;
import com.study.manage.domain.EvaluateJury;
import com.study.manage.domain.Media;
import com.study.manage.domain.User;
import com.study.manage.domain.enumm.BlogType;
import com.study.manage.domain.enumm.LiveDieState;
import com.study.manage.dto.AjaxResponse;
import com.study.manage.dto.SessionUser;
import com.study.manage.mvc.common.UploadController;
import com.study.manage.utils.MapUtils;


@Controller()
@RequestMapping("/v2/jury")
public class V2JuryController extends UploadController {

    @Autowired
    private EvaluateJuryRepository evaluateJuryRepository;
    @Autowired
    private BlogCommentRepository blogCommentRepository;
    @Autowired
    private BlogGradeRepository blogGradeRepository;

    @RequestMapping(path = "list", method = RequestMethod.GET)
    public String article(HttpServletRequest request, ModelMap model,
            HttpServletResponse response) {
        SessionUser su = getUserInfo();
        Date date = new Date();
        model.put("evaluateCount",
                evaluateJuryRepository.countByTeacherIdAndState(su.getId(), LiveDieState.LIVE));
        List<Long> evaluateIds =
                evaluateJuryRepository.findEvaluateIdByTeacherIdAndState(su.getId());
        if (evaluateIds.isEmpty()) {
            evaluateIds.add(0l);
        }
        model.put("blogCommentMothCount", blogCommentRepository
                .countByUserIdAndStateAndMoth(evaluateIds, su.getId(), LiveDieState.LIVE, date));

        model.put("blogGradeMothCount", blogGradeRepository
                .countByUserIdAndStateAndMoth(evaluateIds, su.getId(), LiveDieState.LIVE, date));
        return "v2/jury/list";
    }

    @Autowired
    private UserRepository userRepository;
    @Autowired
    private UserDetailRepository userDetailRepository;


    @Autowired
    private EvaluateRepository evaluateRepository;
    @Autowired
    private EvaluateApplyWorkRepository evaluateApplyWorkRepository;


    @ResponseBody
    @RequestMapping(path = "list-evaluate", method = RequestMethod.GET)
    public AjaxResponse articleList(HttpServletRequest request,
            @PageableDefault(sort = {"createdAt"}, direction = Direction.DESC) Pageable pageable) {
        AjaxResponse ajax = new AjaxResponse(true);
        SessionUser su = getUserInfo();

        Page<EvaluateJury> page =
                evaluateJuryRepository.findEvaluateByTeacherId(su.getId(), pageable);
        return ajax.setObj(page.map(new Converter<EvaluateJury, Map<String, Object>>() {
            @Override
            public Map<String, Object> convert(EvaluateJury source) {
                Evaluate e = evaluateRepository.findOne(source.evaluateId);
                Map<String, Object> map = MapUtils.convert(e);
                map.put("blogCount", evaluateApplyWorkRepository.countByEvaluateIdAndState(e.id,
                        LiveDieState.LIVE));
                map.put("remarks", source.remarks);
                return map;
            }

        }));
    }

    @Autowired
    private BlogRepository blogRepository;
    @Autowired
    private EvaluateApplyRepository evaluateApplyRepository;
    @Autowired
    private BlogCommentMediaRepository blogCommentMediaRepository;



    @ResponseBody
    @RequestMapping(path = "list-comment", method = RequestMethod.GET)
    public AjaxResponse list_comment(HttpServletRequest request, Long evaluateId,
            @DateTimeFormat(pattern = "yyyy-MM") Date date,
            @PageableDefault(sort = {"createdAt"}, direction = Direction.DESC) Pageable pageable) {
        AjaxResponse ajax = new AjaxResponse(true);
        SessionUser su = getUserInfo();
        List<Long> evaluateIds =
                evaluateJuryRepository.findEvaluateIdByTeacherIdAndState(su.getId());
        if (evaluateIds.isEmpty()) {
            evaluateIds.add(0l);
        }
        Page<BlogComment> page = blogCommentRepository.findByUserIdAndStateAndMoth(evaluateIds,
                su.getId(), LiveDieState.LIVE, date, pageable);
        return ajax.setObj(page.map(new Converter<BlogComment, Map<String, Object>>() {
            @Override
            public Map<String, Object> convert(BlogComment source) {
                Map<String, Object> map = MapUtils.convert(source);
                Blog b = blogRepository.findOne(source.blogId);
                List<Media> ms = blogCommentMediaRepository.findByBlogCommentId(source.id);
                if (!ms.isEmpty()) {
                    map.put("mediaUrl", ms.get(0).url);
                    map.put("mediaType", ms.get(0).type);
                }
                if (b.type == BlogType.StudyEvaluate) {
                    EvaluateApply ea = evaluateApplyRepository.findOne(b.extId);
                    Evaluate e = evaluateRepository.findOne(ea.evaluateId);
                    map.put("evaluateTitle", e.title);
                }
                User u = userRepository.findOne(source.blogUserId);
                map.put("userNumber", u.number);
                map.put("userName", u.name);
                return map;
            }

        }));
    }


    @ResponseBody
    @RequestMapping(path = "list-grade", method = RequestMethod.GET)
    public AjaxResponse list_grade(HttpServletRequest request, Long evaluateId,
            @DateTimeFormat(pattern = "yyyy-MM") Date date,
            @PageableDefault(sort = {"createdAt"}, direction = Direction.DESC) Pageable pageable) {
        AjaxResponse ajax = new AjaxResponse(true);
        SessionUser su = getUserInfo();
        List<Long> evaluateIds =
                evaluateJuryRepository.findEvaluateIdByTeacherIdAndState(su.getId());
        if (evaluateIds.isEmpty()) {
            evaluateIds.add(0l);
        }
        Page<BlogGrade> page = blogGradeRepository.findByUserIdAndStateAndMoth(evaluateIds,
                su.getId(), LiveDieState.LIVE, date, pageable);
        return ajax.setObj(page.map(new Converter<BlogGrade, Map<String, Object>>() {
            @Override
            public Map<String, Object> convert(BlogGrade source) {
                Map<String, Object> map = MapUtils.convert(source);
                if (source.evaluateId != null) {
                    Evaluate e = evaluateRepository.findOne(source.evaluateId);
                    map.put("evaluateTitle", e.title);
                }
                int gradeIndex =
                        blogGradeRepository.countByBlogIdAndIsEvaluateJuryAndStateAndMinData(
                                source.blogId, true, source.createdAt);
                map.put("gradeIndex", gradeIndex + 1);
                User u = userRepository.findOne(source.blogUserId);
                map.put("userNumber", u.number);
                map.put("userName", u.name);
                return map;
            }

        }));
    }
}
